<?php
/* 
* @Author: Jerry-chen
* @Date:   2014-07-16 22:41:26
* @Last Modified time: 2014-07-22 22:56:21
* @Last Modified by: Jerry-chen<uranusbill@163.com>
* @ Copyright (c) 2014 http://www.cooldreamer.com All rightsreserved.
*/

class ConfirmControl extends CommonControl
{
	
	public function index()
	{
		//设置一个步骤
		
		if(IS_POST && $_SESSION['step']==2){
			// p($_POST);
			$data = Q('post.');
			//订单号生成格式
			$ordernumber='datami'.time();
			// 生成订单
				$orderData = array(
					'ordernumber'=>$ordernumber,
					'uid'=>session('uid'),
					'stadus'=>0,
					'ordertime'=>time(),
					);
				$orderid = K('order')->add_($orderData);

			foreach ($data as $k => $v) {
				if($k=='number')
				{
					// 筛选掉K值为NUMBER的值
					continue;
				}
				$k =substr($k, 10);
				// 根据SHOPCARID查出SHOPCAR商品 
				$orderlist =K('Shopcar')->get_one(array('shopcarid'=>$k)); 
				// p($orderlist);
				//查出价格，算出小计
				$priceAttr  =K('Goodsprice')->get_one(array('goodsprice'=>$orderlist['goodsprice']),'price');
				$recount = $priceAttr['price']*$orderlist['number'];
				
				$orderlistData = array(
					'orderid'=>$orderid,
					'goodsid'=>$orderlist['goodsid'],
					'timeid'=>$orderlist['timeid'],
					'number'=>$orderlist['number'],
					'recount'=>$recount,
					'price'=>$priceAttr['price'],
					);
				K('Orderlist')->add_($orderlistData);
				//订单步骤
				session('step',3);
				//方便后面刷取值
				session('orderid',$orderid);
				//删除相应的购物车商品
				K('Shopcar')->del_(array('shopcarid'=>$orderlist['shopcarid']));
			}
		}
			//根据订单号查出同一订单的商品，然后分配数据
			$orderdata = K('Order')->get_all(array('uid'=>session('uid'),'orderid'=>session('orderid')));
			// p($orderdata);
			$count = 0;
			foreach ($orderdata as $k => $v) {
				$orderlist = K('Orderlist')->get_all(array('orderid'=>$v['orderid']),array('timeid','goodsid','price','number','recount'));
				// p($orderlist);

				$orderdata[$k]['orderlist'] = $orderlist;
					foreach ($orderlist as $key => $value) {
						// 根据时间ID，查出时间
					$timename = K('Goodstime')->get_one(array('timeid'=>$value['timeid']),array('time'));
					$orderdata[$k]['orderlist'][$key]['time'] = $timename['time'];
					//根据货品ID查出货物名
					$goodsname = K('Goods')->get_one(array('goodsid'=>$value['goodsid']),array('goodsname'));
					$orderdata[$k]['orderlist'][$key]['goodsname'] = $goodsname['goodsname'];
					//价格
					$orderdata[$k]['orderlist'][$key]['price'] = $value['price'];
					$orderdata[$k]['orderlist'][$key]['number'] = $value['number'];
					$orderdata[$k]['orderlist'][$key]['recount'] = $value['recount'];
					$count += $value['recount'];
					//订单合计
					}
					
			}
			//分配订单数据
			$this->assign('orderdata',$orderdata);
			$this->assign('count',$count);

		
		
		//收货地址管理
		$spData = K('Spaddress')->get_one(array('uid'=>session('uid')));
		$this->assign('spData',$spData);
		// p($spData);

				// p($value);
				
				// //根据时间ID，查出时间
				// $timename = K('Goodstime')->get_one(array('timeid'=>$value['timeid']),array('time'));
				// $orderData[$key]['time'] = $timename['time'];
				// //算出小计
				// $orderData[$key]['recount'] = $value['price']*$value['number'];
				// p($orderData);
		// // 根据得到商品ID查出商品
		// $goodsname = K('Goods')->get_one(array('goodsid'=>Q('post.goodsid')),array('goodsname','goodsid'));
		// // 分配数据
		// $this->assign('goodsname',$goodsname);
		// // 根据时间ID，查出时间
		// $timename = K('Goodstime')->get_one(array('timeid'=>Q('post.timeid')),array('time'));
		// // 分配数据
		// $this->assign('timename',$timename);
		// // p($timename);
		// // 根据单价ID查出单价
		// $goodspricedata = K('Goodsprice')->get_one(array('goodsprice'=>Q('post.goodsprice')),array('goodstimeid','countticket','price'));
		// //订单数量，压入到商品价格数据，方便分配
		// $goodspricedata['number'] = Q('post.number');
		// //小计
		// $goodspricedata['count'] = $goodspricedata['price']*Q('post.number');
		// // 分配数据
		// $this->assign('goodspricedata',$goodspricedata);
		// p($goodspricedata);
		$this->display('confirm.html');
	}
}